<?php
class Download extends AppModel {

    public $validate = array(
    'summary' => array('notEmpty'),
    );

    /**
     * Prepares file's data to save
     *
     * @param $data
     * @param $SwfUpload
     * @return prepared data
     */
    public function prepare_file_data($data,&$SwfUpload) {
        $file = array();
        $file['Download']['file'] = $SwfUpload->filename;
        $file['Download']['size'] = $data['Download']['file']['size'];
        $file['Download']['type'] = $data['Download']['file']['type'];
        $file['Download']['summary'] = $data['Download']['summary'];
        $file['Download']['md5'] = md5($SwfUpload->filename);

        return $file;
    }

    public function delete_file($id) {
        App::import('vendor','file');
        $name = $this->findById($id,'file');
        $path = WWW_ROOT.'files'.DS.$name['Download']['file'];
        $file =& new File($path);
        return $file->delete();
    }

    public function delete_files($files) {
        foreach ($files as $file) {
            $this->delete_file($file);
        }
        return $this->deleteAll(array('Download.id'=>$files));
    }

    public function last() {
        return $this->Download->findAll(null,array('Download.summary','Download.file','Download.size'),'Download.id desc',Configure::read('Homepage.downloads'));
    }
}
?>